var app = getApp();//引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var page = 1;//页码
var GZHid = 53;//公众号id
var dataLists = {};//缓存商品列表数据
//var commonURL=appjs.data.baseURL;
Page({
  data: {
    cid: "",//分类id
    ctxt: "",//分类名称，用于点击时更换标题
    imgUrls: [],
    navbar: [],
    userpasswd: {},
    hehuoren: false,
    xiaoqudw: '',
    navbarTop: [
      { name: '拼团推荐', id: 2, url: '../recommend/recommend' },
      { name: '商城小区', id: 3, url: '../mall_village/mall_village' },
      { name: '预约活动', id: 4, url: '../jielong_list/jielong_list' },
      { name: '爆品接龙', id: 5, url: '../jielong_list/jielong_list' }
    ],
    countDownDay: '00',
    countDownHour: '00',
    countDownMinute: '00',
    countDownSecond: '00',
    lists: [
      // {
      //   "img":"../../images/index/detail1.jpg",
      //   "title":"测试测试测试测试测试测试测试测试测试测试测试测试测试测试",
      //   "price":"2000.00",
      //   "peoplenum":"2000",
      //   "unit":"公斤",
      //   "smtuan":"随意团"
      // }
    ],
    totalPrice: 0,   //合计价格
    count: 0,        //合计总数
    showDialog: false,
    showDetail: false,
    selectProduct: {},  //选择的商品
    opstions: [],      //规格项
    guige: {
      price: "0.00",
      list: [
        {
          id: "",
          name: ""
        }
      ]
    },
    guigeOptions: [
      // {
      //   costprice:"0.03",
      //   id:"8697",
      //   marketprice:"0.01",
      //   productprice:"0.02",
      //   specs:"4110",
      //   stock:"11105",
      //   thumb:"",
      //   title:"黄"
      // }
    ],
    currentItem: [
      // {
      //   title: "",
      //   item: "-1"
      // }
    ],
    num_count: 1
  },
  onLoad: function () {
    // //调用应用实例的方法获取全局数据
    //that.ajaxLists(page,that.data.cid);
    // wx.request({
    //     url: commonURLYY + 'op=fenlei',
    //     data: {
    //         page: page
    //     },
    //     header: {
    //         'content-type': 'application/json'
    //     },
    //     success: function (res) {
    //       console.log(res.data.menuList,888888888)
    //         that.setData({
    //             navbarTop: res.data.menuList
    //         })
    //         // this.data.navbarTop = res.data.menuList;
    //         //   console.log(res.data.menuList)
    //     }
    // })
    var that = this;
    if (that.xiaoqudw != '') {
      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function (res) {
          var latitude = res.latitude
          var longitude = res.longitude
          wx.request({//获取小区地址
            url: commonURL + 'op=shop_area',
            data: { lat: res.latitude, lng: res.longitude, uniacid: app.data.GZHid },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res, '获取小区')
              that.setData({
                region: res.data,
                xiaoqudw: res.data.name
              });
            }
          })
        }
      })
    }
    wx.request({
      url: commonURLXX + 'op=uniacid_shop',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.setNavigationBarTitle({ title: res.data.sname });//设置title文字
      }
    })
  },
  handler: function (e) {
    if (e.detail.authSetting["scope.userLocation"]) {//如果打开了地理位置，就会为true
      this.setData({
        showFlag: true
      })
      var that = this;
      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function (res) {
          var latitude = res.latitude
          var longitude = res.longitude
          wx.request({//获取小区地址
            url: commonURL + 'op=shop_area',
            data: { lat: res.latitude, lng: res.longitude, uniacid: app.data.GZHid },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res, '获取小区')
              that.setData({
                region: res.data,
                xiaoqudw: res.data.name,
                xiaoquID: res.data.id
              });
            }
          })
        }
      })
    }
  },
  modalcnt: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否退出合伙人登录',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.removeStorage({
            key: 'userpasswd',
            success: function (res) {
              console.log(res.data)
              that.setData({
                userpasswd: {},
                hehuoren: false
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 上拉加载回调接口
  onReachBottom: function () {
    var that = this;
    // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
    // 上拉时需把total在原来的基础上加上count，代表从count条后的数据开始请求。
    page++;
    var areaid = ''
    if (that.xiaoqudw != '') {
      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function (res) {
          var latitude = res.latitude
          var longitude = res.longitude
          wx.request({//获取小区地址 
            url: commonURL + 'op=shop_area',
            data: { lat: res.latitude, lng: res.longitude, uniacid: app.data.GZHid },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res.data.id, '获取小区')
              areaid = res.data.id
              wx.request({//加载商品列表
                url: commonURLYY + 'op=ajax',
                data: {
                  page: page,
                  pagesize: 4,
                  cid: that.data.cid,
                  areaid: areaid
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  if (res.data.list.length < 1) {
                    wx.showToast({
                      title: '全部商品加载完成',
                      icon: 'success',
                      duration: 1000
                    })
                    return false;
                  }
                  wx.showToast({
                    title: '商品加载中...',
                    icon: 'loading',
                    duration: 700
                  })
                  for (var i = 0, lists = []; i < res.data.list.length; i++) {
                    var obj = {};
                    var indexid = obj["id"] = res.data.list[i].id;
                    obj["img"] = res.data.list[i].gimg;
                    obj["title"] = res.data.list[i].gname;
                    obj["price"] = res.data.list[i].oprice;//单买价
                    obj["gprice"] = res.data.list[i].gprice;//团购价
                    obj["peoplenum"] = res.data.list[i].groupnum;
                    obj["self_time_start"] = that.fmtDate(Number(res.data.list[i].self_time_start))
                    obj["self_time_end"] = that.fmtDate(Number(res.data.list[i].self_time_end))
                    obj["is_self_time"] = res.data.list[i].is_self_time
                    obj["unit"] = res.data.list[i].unit;
                    obj["mprice"] = res.data.list[i].mprice;//市场价
                    obj["salenum"] = res.data.list[i].salenum;
                    obj["xiangqing"] = res.data.list[i].gdesc;
                    obj["shuxing"] = res.data.list[i].params;
                    obj["advs"] = res.data.list[i].advs;
                    obj["kucun"] = res.data.list[i].gnum;
                    obj["selltype"] = res.data.list[i].selltype;
                    obj["hasoption"] = res.data.list[i].hasoption;//是否有规格 0没有 ，1有
                    obj["weight"] = res.data.list[i].weight;//重量，用于计算运费
                    obj["deliverytype"] = res.data.list[i].deliverytype;//配送方式
                    obj["jielong"] = res.data.list[i].jielong;//接龙
                    // numTime()

                    if (res.data.list[i].isshow == '3' || res.data.list[i].gnum == "0") {
                      obj["smtuan"] = "售罄";
                      obj["backg"] = "#aaa";
                      obj["url"] = '';
                    } else {
                      if (res.data.list[i].selltype == '0') {
                        obj["url"] = '../index_only_detail/index_only_detail?id=' + indexid;
                      } else {
                        if (res.data.list[i].jielong == '0') {
                          obj["smtuan"] = that.select_tuan(res.data.list[i].selltype);
                          obj["url"] = '../index_detail/index_detail?id=' + indexid;
                        } else {
                          obj["smtuan"] = that.select_tuan(res.data.list[i].selltype);
                          obj["url"] = '../detail_jielong/detail_jielong?id=' + indexid;
                        }
                      }
                    }
                    lists.push(obj);
                    dataLists[indexid] = obj;
                  }
                  that.setData({
                    lists: that.data.lists.concat(lists)
                  });
                  wx.getStorage({
                    key: 'indexLists',
                    success: function (res) {
                      var data = res.data[indexid];
                      if (!res.data[indexid]) {//如果缓存中不存在对应的商品，就ajax加载数据
                        wx.setStorage({
                          key: "indexLists",
                          data: dataLists
                        })
                      }
                    }
                  })
                }
              })
            }
          })
        }
      })
    }

  },
  upper: function () {
    this.ajaxLists(1, "");
    wx.setNavigationBarTitle({ title: '首页' })
  },
  leixingQH: function (e) {//点击类型加载不同数据
    var that = this;
    page = 1;
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var Bartitle = e.currentTarget.dataset.title;
    if (id === that.data.cid) return false;
    that.setData({ cid: id })
    console.log(id);
    wx.request({//根据分类加载商品列表
      url: commonURLYY + 'op=ajax',
      data: {
        page: page,
        pagesize: 4,
        cid: id,
        areaid: areaid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.setNavigationBarTitle({ title: Bartitle });//设置title文字
        for (var i = 0, lists = []; i < res.data.list.length; i++) {
          var obj = {};
          var indexid = obj["id"] = res.data.list[i].id;
          obj["img"] = res.data.list[i].gimg;
          obj["title"] = res.data.list[i].gname;
          obj["price"] = res.data.list[i].oprice;//单买价
          obj["gprice"] = res.data.list[i].gprice;//团购价
          obj["peoplenum"] = res.data.list[i].groupnum;
          obj["self_time_start"] = that.fmtDate(Number(res.data.list[i].self_time_start))
          obj["self_time_end"] = that.fmtDate(Number(res.data.list[i].self_time_end))
          obj["is_self_time"] = res.data.list[i].is_self_time
          obj["unit"] = res.data.list[i].unit;
          obj["mprice"] = res.data.list[i].mprice;//市场价
          obj["salenum"] = res.data.list[i].salenum;
          obj["xiangqing"] = res.data.list[i].gdesc;
          obj["shuxing"] = res.data.list[i].params;
          obj["advs"] = res.data.list[i].advs;
          obj["kucun"] = res.data.list[i].gnum;
          obj["selltype"] = res.data.list[i].selltype;
          obj["hasoption"] = res.data.list[i].hasoption;//是否有规格 0没有 ，1有
          obj["weight"] = res.data.list[i].weight;//重量，用于计算运费
          obj["deliverytype"] = res.data.list[i].deliverytype;//配送方式
          obj["jielong"] = res.data.list[i].jielong;//接龙
          obj["jielong"] = res.data.list[i].jielong;
          if (res.data.list[i].isshow == '3' || res.data.list[i].gnum == "0") {
            obj["smtuan"] = "售罄";
            obj["backg"] = "#aaa";
            obj["url"] = '';
          } else {
            if (res.data.list[i].selltype == '0') {
              obj["url"] = '../index_only_detail/index_only_detail?id=' + indexid;
            } else {
              if (res.data.list[i].jielong == '0') {
                obj["smtuan"] = that.select_tuan(res.data.list[i].selltype);
                obj["url"] = '../index_detail/index_detail?id=' + indexid;
              } else {
                obj["smtuan"] = that.select_tuan(res.data.list[i].selltype);
                obj["url"] = '../detail_jielong/detail_jielong?id=' + indexid;
              }
            }
          }
          lists.push(obj);
          dataLists[indexid] = obj;
          wx.getStorage({
            key: 'indexLists',
            success: function (res) {
              var data = res.data[indexid];
              //console.log(res.data);
              if (!res.data[indexid]) {//如果缓存中不存在对应的商品，就ajax加载数据
                wx.setStorage({
                  key: "indexLists",
                  data: dataLists
                })
              }
            }
          })
        }
        that.setData({
          lists: lists
        })

      }
    })
  }, 
  fmtDate: function (timeStamp) {//时间搓转换年月日
    var date = new Date(); date.setTime(timeStamp * 1000); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return y + '-' + m + '-' + d;
  },
  onShow: function () {//页面显示加载数据
    page = 1;
    var that = this;
    // wx.getUserInfo({});
    wx.request({//banner
      url: commonURL + 'op=banner',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)

        for (var i = 0, banner = []; i < res.data.length; i++) {
          banner.push(res.data[i].thumb);
        }
        that.setData({
          "imgUrls": banner,
        });
      }
    })
    wx.getStorage({
      key: 'userpasswd',
      success: function (res) {
        if (res.data) {
          that.setData({
            userpasswd: res.data,
            hehuoren: true
          })
        }

      }
    })
    // wx.getLocation({
    //   type: 'gcj02', //返回可以用于wx.openLocation的经纬度
    //   success: function (res) {
    //     var latitude = res.latitude
    //     var longitude = res.longitude
    //     // console.log(res)
    //     // wx.openLocation({
    //     //   latitude: latitude,
    //     //   longitude: longitude,
    //     //   scale: 28
    //     // })
    //     that.setData({
    //       lat: res.latitude,
    //       lng: res.longitude
    //     });

    //   }
    // })
    // wx.request({//获取小区地址
    //   url: commonURL + 'op=shop_area',
    //   data: { lat: that.data.lat, lng: that.data.lng, uniacid: app.data.GZHid },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {

    //     console.log(res,that.data.lat,that.data.lng, '获取小区')
    //     that.setData({
    //       region: res.data,
    //     });
    //   }
    // })
    wx.request({//商品分类
      url: commonURL + 'op=category',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        for (var i = 0, navbar = []; i < res.data.length; i++) {
          var obj = {};
          obj["img"] = res.data[i].smallthumb;
          obj["txt"] = res.data[i].name;
          obj["id"] = res.data[i].id;
          navbar.push(obj);
        }
        that.setData({
          "navbar": navbar,
        });
      }
    })
    this.ajaxLists(page, this.data.cid);
  },
  onShareAppMessage: function () {
    return {
      title: this.data.ctxt,
      path: '/pages/index/index'
    }
  },
  onPullDownRefresh: function () {//页面下拉加载首页数据
    this.ajaxLists(1, "");
    wx.setNavigationBarTitle({ title: '首页' })
  },
  ajaxLists: function (num, cid) {
    var that = this;
    page = num
    var areaid = ''
    if (that.xiaoqudw != '') {
      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function (res) {
          var latitude = res.latitude
          var longitude = res.longitude
          wx.request({//获取小区地址
            url: commonURL + 'op=shop_area',
            data: { lat: res.latitude, lng: res.longitude, uniacid: app.data.GZHid },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res.data.id, '获取小区')
              areaid = res.data.id
              wx.request({//加载商品列表
                url: commonURLYY + 'op=ajax',
                data: {
                  page: num,
                  pagesize: 4,
                  cid: cid,
                  areaid: areaid
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  setInterval(function () {

                    var num_timearr = [];
                    for (var i = 0, lists = []; i < res.data.list.length; i++) {
                      var obj = {};
                      var indexid = obj["id"] = res.data.list[i].id;

                      var timenum = Number(res.data.list[i].spike_end)
                      var totalSecond = timenum - Date.parse(new Date()) / 1000; //这边传一个时间戳

                      // 秒数
                      var second = totalSecond;

                      // 天数位
                      var day = Math.floor(second / 3600 / 24);
                      var dayStr = day.toString();
                      if (dayStr.length == 1) dayStr = '0' + dayStr;

                      // 小时位
                      var hr = Math.floor((second - day * 3600 * 24) / 3600);
                      var hrStr = hr.toString();
                      if (hrStr.length == 1) hrStr = '0' + hrStr;

                      // 分钟位
                      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
                      var minStr = min.toString();
                      if (minStr.length == 1) minStr = '0' + minStr;

                      // 秒位
                      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
                      var secStr = sec.toString();
                      if (secStr.length == 1) secStr = '0' + secStr;
                      var num_time = {};
                      num_time = { dayStr: dayStr, hrStr: hrStr, minStr: minStr, secStr: secStr }
                      obj["spike_end"] = res.data.list[i].spike_end
                      obj["count_down"] = num_time
                      obj["img"] = res.data.list[i].gimg;
                      obj["title"] = res.data.list[i].gname;
                      obj["price"] = res.data.list[i].oprice;//单买价
                      obj["gprice"] = res.data.list[i].gprice;//团购价
                      obj["peoplenum"] = res.data.list[i].groupnum;
                      obj["self_time_start"] = that.fmtDate(Number(res.data.list[i].self_time_start))
                      obj["self_time_end"] = that.fmtDate(Number(res.data.list[i].self_time_end))
                      obj["is_self_time"] = res.data.list[i].is_self_time
                      obj["unit"] = res.data.list[i].unit;
                      obj["mprice"] = res.data.list[i].mprice;//市场价
                      obj["salenum"] = res.data.list[i].salenum;
                      obj["xiangqing"] = res.data.list[i].gdesc;
                      obj["shuxing"] = res.data.list[i].params;
                      obj["advs"] = res.data.list[i].advs;
                      obj["kucun"] = res.data.list[i].gnum;
                      obj["selltype"] = res.data.list[i].selltype;
                      obj["hasoption"] = res.data.list[i].hasoption;//是否有规格 0没有 ，1有
                      obj["weight"] = res.data.list[i].weight;//重量，用于计算运费
                      obj["deliverytype"] = res.data.list[i].deliverytype;//配送方式
                      obj["jielong"] = res.data.list[i].jielong;//接龙
                      if (res.data.list[i].isshow == '3' || res.data.list[i].gnum == "0") {
                        obj["smtuan"] = "售罄";
                        obj["backg"] = "#aaa";
                        obj["url"] = '';
                      } else {
                        if (res.data.list[i].selltype == '0') {
                          obj["url"] = '../index_only_detail/index_only_detail?id=' + indexid;
                        } else {
                          if (res.data.list[i].jielong == '0') {
                            obj["smtuan"] = that.select_tuan(res.data.list[i].selltype);
                            obj["url"] = '../index_detail/index_detail?id=' + indexid;
                          } else {
                            obj["smtuan"] = that.select_tuan(res.data.list[i].selltype);
                            obj["url"] = '../detail_jielong/detail_jielong?id=' + indexid;
                          }
                        }
                      }
                      if (res.data.list[i].spike_end <= Date.parse(new Date()) / 1000) {
                        obj["url"] = '';
                        obj["backg"] = "#aaa";
                        obj["smtuan"] = '活动结束'
                      }
                      // if (res.data.list[i].spike_start <= Date.parse(new Date()) / 1000) {
                      //   obj["url"] = '';
                      //   obj["backg"] = "#aaa";
                      //   obj["smtuan"] = '活动尚未开始'
                      // }
                      lists.push(obj);
                      dataLists[indexid] = obj;
                    }
                    that.setData({
                      lists: lists,
                      cid: ""
                    });

                    wx.setStorage({
                      key: "indexLists",
                      data: dataLists
                    })
                  }, 1000)
                }

              })
            }
          })
        }
      })
    }
  },
  showDetail: function (event) {   //规格
    var that = this;
    var { hasoption, good } = event.currentTarget.dataset;
    var id = event.currentTarget.dataset.id;
    if (good.smtuan == '售罄') return;
    this.setData({
      num_count: 1
    })

    if (hasoption == '0') {
      wx.request({
        url: commonURLYY + 'op=cart_add',
        data: {
          openid: app.data.openid,
          gid: id
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.status == -1) {
            wx.showToast({
              title: res.data.result,
              duration: 1000
            })
          } else {
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 1000
            })
          }
        }
      })
    } else {
      this.data.showDetail = !this.data.showDetail;
      let good = event.currentTarget.dataset.good;
      console.log(good);
      this.setData({
        showDetail: this.data.showDetail,
        selectProduct: good
      })
      this.ajaxGuie(this);
    }
  },
  addSelect: function (event) {
    let good = event.currentTarget.dataset.good;
    this.data.count++
    // if(good.gprice){
    //   this.data.totalPrice += Number(good.gprice);
    // }else{
    //   this.data.totalPrice += Number(good.gprice);
    // };
    // let num = this.data.totalPrice;
    // let totalPrice = Math.round(num*100)/100;
    // this.setData({
    //   count: this.data.count
    // })
  },
  showDialog: function () {     //购物车详情弹窗
    this.data.showDialog = !this.data.showDialog;
    var num_count = this.data.num_count;
    num_count = 1;
    this.setData({
      showDialog: this.data.showDialog,
      num_count: num_count
    })
  },
  chooseGG: function (event) {  //选择规格
    var that = this;
    var title = event.currentTarget.dataset.title;
    var options = this.data.guigeOptions;
    var currentItem = this.data.currentItem;
    var key = event.currentTarget.dataset.key;
    var guige = this.data.guige;
    currentItem[key].title = title;
    currentItem[key].item = event.currentTarget.dataset.index;
    for (var i = 0; i < options.length; i++) {
      for (var j = 0, ii = 0; j < currentItem.length; j++) {
        if (currentItem[j].title == '') {
          that.setData({
            'currentItem': currentItem,
            'guige': guige
          })
          return false;
        }
        var patt1 = new RegExp(currentItem[j].title);
        var result = patt1.test(options[i].title);
        result && ii++;
      }
      if (currentItem.length === ii) {
        that.data.istuan ? guige.price = options[i].marketprice : guige.price = options[i].productprice;
      }
    }
    this.setData({
      currentItem: currentItem,
      guige: guige
    })
  },
  ajaxGuie: function (that) {
    var ajaxguige = wx.getStorageSync('guige');
    if (ajaxguige.id === that.data.selectProduct.id) {//先判断该商品规格是否存在缓存中，否则ajax加载数据
      var data = ajaxguige.allspecs;
      var currentItem = [];
      for (var i = 0; i < data.length; i++) {
        currentItem.push({ title: "", item: "-1" });
      }
      if (data.length < 1) {
        return false
      }
      that.setData({
        guige: {
          price: "0.00",
          list: data
        },
        guigeOptions: ajaxguige.options,
        currentItem: currentItem
      })
    } else {
      wx.request({
        url: commonURL + 'op=goods_guige',
        data: {
          id: this.data.selectProduct.id
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          var data = res.data.allspecs;
          var currentItem = [];
          if (data.length < 1) return;
          for (let i = 0; i < data.length; i++) {
            currentItem.push({ title: "", item: "-1" })
          }
          that.setData({
            guige: {
              price: '0.00',
              list: data
            },
            guigeOptions: res.data.options,
            currentItem: currentItem
          })
          var dataguige = res.data;
          dataguige.id = that.data.selectProduct.id;
          wx.setStorage({               //把规格存储在缓存中
            key: 'guige',
            data: dataguige
          })
        }
      })
    }
  },
  confirm: function (e) {
    // console.log(this.data.currentItem);
    var that = this;
    for (var i = 0; i < that.data.currentItem.length; i++) {
      if (that.data.currentItem[i].item == "-1") {//判断是否选中规格
        wx.showToast({
          title: '请选择规格',
          icon: 'loading',
          duration: 1500
        })
        return false;
      }
    }
    var _guiges = [];
    var _currentItem = this.data.currentItem;
    var _id = e.currentTarget.dataset.id;
    _currentItem.forEach(function (item) {
      _guiges.push(item.title);
    })
    // console.log(this.data.guige);
    var _cid = this.data.guige.list[0].id
    console.log(_guiges);
    console.log(_cid);
    this.data.totalPrice += Number(this.data.guige.price);
    this.data.showDetail = false;
    this.setData({
      showDetail: this.data.showDetail,
      totalPrice: this.data.totalPrice
    })

    wx.request({
      url: commonURLYY + 'op=cart_add',
      data: {
        openid: app.data.openid,
        gid: this.data.selectProduct.id,
        guige: _guiges.join('+'),
        num: this.data.num_count
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == -1) {
          wx.showToast({
            title: res.data.result,
            icon: 'loading',
            duration: 1000
          })
          return false;
        }
        if (_id = '2') {
          wx.navigateTo({
            url: '../index_cart/index_cart'
          })
        } else {

          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 1000
          })

        }
      }
    })

  },
  // 导航详情
  goNavBarDetail: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/navbar-detail/navbar-detail?index=' + index + '&id=' + id
    })
  },
  reduceCount: function () {
    var num_count = this.data.num_count;
    if (num_count <= 1) {
      num_count = 1;
    } else {
      num_count--;
    }
    this.setData({
      num_count: num_count
    })
  },
  addCount: function () {
    var num_count = this.data.num_count;
    num_count++;
    this.setData({
      num_count: num_count
    })
  },
  select_tuan: function (selltype) {
    var selltype_text = '';
    switch (selltype) {
      case '1':
        selltype_text = '去开团  >';
        break;
      case '2':
        selltype_text = '去开团  >';
        break;
      case '3':
        selltype_text = '去开团  >';
        break;
      case '4':
        selltype_text = '去开团  >';
        break;
      case '5':
        selltype_text = '去开团  >';
        break;
      case '6':
        selltype_text = '去开团  >';
        break;
      case '7':
        selltype_text = '去开团  >';
    }
    return selltype_text = '去开团';
  }
  // send_num: function (num) {
  //   wx.request({
  //     url: commonURLYY+ 'op=cart_add',
  //     data: {
  //       num: num
  //     },
  //     header:{
  //       'content-type': 'application/json'
  //     },
  //     success:function (res) {

  //     }
  //   })
  // }

})
